package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;

import metagrammar.Type;

public class TypeComboBox extends JComboBox<Type> {
    
    private class ModelUpdater implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            updateModel();
        }
    }
    
    private final TypeModelProvider modelProvider;
    
    TypeComboBox(TypeModelProvider modelProvider)
    {
        this.modelProvider = modelProvider;
        updateModel();
    }
    
    /**
     * @param libraryComboBox TODO
     * 
     */
    TypeComboBox(LibraryComboBox libraryComboBox)
    {
        this((TypeModelProvider) libraryComboBox);
        libraryComboBox.addActionListener(new ModelUpdater());
    }
    
    /**
     * 
     */
    private void updateModel()
    {
        setModel(modelProvider.getTypeModel());
        if(getItemCount() > 0) {
            setSelectedIndex(0);
            setPrototypeDisplayValue(null);
        }
        else {
            setPrototypeDisplayValue(metagrammar.Type.ROOT);
        }
    }
}